import { debug } from "util";

/**
 * 请求组件封装
 * @param url {}
 */
function request({url,data,method="GET",contentType=1}){
	let header= {
		'content-type': contentType===1 ? 'application/json':'application/x-www-form-urlencoded'
	}
	// #ifndef H5
		url = "https://music.163.com" +url
	// #endif
	console.log('request url:',url)
	return new Promise((resolve,reject)=>{
		uni.request({
			url:url,
			data,
			method,
			header,
			success:(res)=>{
				if(res.statusCode ===200){
					//请求成功
					resolve(res.data);
				}else if (res.statusCode ===401){
					uni.showToast({
						icon:'none',
						title:'未登录或登录状态已超时',
						duration:1500
					});
				}else if (res.statusCode ===405){
					uni.showToast({
						icon:'none',
						title:'请求方法错误',
						duration:1500
					});
				}else {
					uni.showToast({
						icon:'none',
						title:'请求错误:'+ res.statusCode,
						duration:1500
					});
				}
			},
			fail: (err) => {
				console.log("err:",err)
				uni.showToast({
					icon:'none',
					title:err.errMsg,
					duration:1500
				});
				reject(err);
			}
		})
	})
}

export default {request}